package org.example.student_dormitory.service.impl;

import org.example.student_dormitory.dao.DormitoryDao;
import org.example.student_dormitory.dao.impl.DormitoryDaoImpl;
import org.example.student_dormitory.dto.Result;
import org.example.student_dormitory.entity.Admin;
import org.example.student_dormitory.entity.Building;
import org.example.student_dormitory.entity.Dormitory;
import org.example.student_dormitory.service.DormitoryService;

import java.util.List;

public class DormitoryServiceImpl implements DormitoryService {
    private DormitoryDao dormitoryDao = new DormitoryDaoImpl();

    @Override
    public Result loadDormitory(String page, String pageSize, String key, String value) {
//        System.out.println("2已经进入Service");
        int page_int = Integer.parseInt(page);
        int pageSize_int = Integer.parseInt(pageSize);
        //根据value值来判断是否是有条件的分页
        List<Dormitory> list = null;
//        System.out.println("3准备判断然后进入Dao");
        if ("".equals(value) || value == null) {
            list = dormitoryDao.selectAll(page_int,pageSize_int);
        }else {
            list = dormitoryDao.selectAll(page_int,pageSize_int,key,value);
        }
        if (list == null || list.size() == 0) {
//            System.out.println("6service没拿到数据");
            return new Result<>(100,"没有数据",null);
        }else {
//            System.out.println("6service拿到了数据");
            return new Result<>(200,"有数据",list);
        }
    }

    @Override
    public Result loadBuildings() {
        List<Building> buildings = dormitoryDao.selectBuildings();
        if (buildings == null || buildings.size() == 0){
            return new Result<>(100,"没有数据",null);
        }else {
            return new Result<>(200,"有数据",buildings);
        }
    }

    @Override
    public Result checkDormitoryName(String name, String buildingId) {
        //校验宿舍号
        int buildId = Integer.parseInt(buildingId);
        boolean b = dormitoryDao.selectByBuildingIdAndName(name, buildId);
        if (b){
            return new Result<>(200,"可用",null);
        }else {
            return new Result<>(100,"不可用,宿舍已存在",null);
        }
    }

    @Override
    public Result addDormitory(String name, String buildingId, String dormitoryType) {
        Dormitory dormitory = new Dormitory();
        dormitory.setName(name);
        dormitory.setNumber(Integer.parseInt(dormitoryType));
        dormitory.setSparebed(Integer.parseInt(dormitoryType));
        dormitory.setBuildingId(Integer.parseInt(buildingId));
        dormitory.setDr(0);
        boolean b = dormitoryDao.insertDormitory(dormitory);
        if (b){
            return new Result<>(200,"添加成功",null);
        }else {
            return new Result<>(100,"添加失败",null);
        }
    }

    @Override
    public Result updateDormitory(String id, String dormitoryName, String spareBed, String typeNumber) {
        Dormitory dormitory = new Dormitory();
        dormitory.setId(Long.parseLong(id));
        dormitory.setName(dormitoryName);
        dormitory.setSparebed(Long.parseLong(spareBed));
        dormitory.setNumber(Long.parseLong(typeNumber));
//        dormitory.setBuildingId(Long.parseLong(building));

        System.out.println("准备进入Dao");
        boolean update = dormitoryDao.update(dormitory);
        if (update){
            System.out.println("修改成功");
            return new Result<>(200,"修改成功",null);
        }else {
            System.out.println("修改失败");
            return new Result<>(100,"修改失败",null);
        }
    }

    @Override
    public Result deleteById(String id) {
        Boolean delete = dormitoryDao.deleteById(Integer.parseInt(id));
        if (delete){
            return new Result<>(200,"删除成功", null);
        }else {
            return new Result<>(100,"删除失败", null);
        }
    }
}
